package com.demo.config;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Classname SpringContextUtil
 * @Description 获取上下文
 * @Date 2023/8/9 20:54
 * @Created by Leo825
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    /**
     * 设置上下文
     * @param context
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }
    /**
     * 获取上下文
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过名字获取上下文中的bean
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        if (name == null || name.length() == 0) {
            return null;
        }
        try {
            String beanName = "";
            if (name.length() > 1) {
                beanName = name.substring(0, 1).toLowerCase() + name.substring(1);
            } else {
                beanName = name.toLowerCase();
            }
            return applicationContext.getBean(beanName);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 通过类型获取上下文中的bean
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        try {
            return (T) applicationContext.getBean(clazz);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}